<?php
namespace app\appclass\model;
use app\BaseModel;
use think\facade\Db;



class CardWater extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_appclass_card_water';


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:00
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:03
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:04
     * @功能说明:详情
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 16:05
     * @功能说明:
     */
    public function dataList($dis,$page=10){

        $data = $this->where($dis)->where('status','>',-1)->order('id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-20 11:08
     * @功能说明:添加预约流水
     */
    public function recordWaterAdd($record_id,$true_value,$card_id,$add=0){

        $card_model = new CardUser();

        $card = $card_model->dataInfo(['id'=>$card_id]);

        $insert = [

            'uniacid' => $card['uniacid'],

            'card_id' => $card_id,

            'o_value' => $card['true_value'],

            'value'   => $true_value,

            'n_value' => $add==0?$card['true_value'] - $true_value:$card['true_value'] + $true_value,

            'record_id'=> $record_id,

            'add'      => $add,

        ];

        $res = $this->dataAdd($insert);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-09 11:19
     * @功能说明:后台或者员工
     */
    public function controlWater($card,$value,$text='',$controller=''){

        if($value!=$card['true_value']){

            $add    = $value>$card['true_value']?1:0;

            $insert = [

                'uniacid' => $card['uniacid'],

                'card_id' => $card['id'],

                'o_value' => $card['true_value'],

                'n_value' => $value,

                'value'   => abs($value-$card['true_value']),

                'add'     => $add,

                'controller'=> $controller,

                'type'    => 1,

                'text'    => $text,
            ];

            $res = $this->dataAdd($insert);

            return $res;
        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-01 09:42
     * @功能说明:续开变更流水记录
     */
    public function renewCardWater($card,$value,$text='',$controller=''){


        $insert = [

            'uniacid' => $card['uniacid'],

            'card_id' => $card['id'],

            'o_value' => $card['true_value'],

            'n_value' => $value+$card['true_value'],

            'value'   => $value,

            'add'     => 1,

            'controller'=> $controller,

            'type'    => 2,

            'text'    => $text,
        ];

        $res = $this->dataAdd($insert);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2021-06-09 11:11
     * @功能说明:流水记录
     */
    public function waterList($dis,$page=10){

        $data = $this->where($dis)->order('id desc')->paginate($page)->toArray();

        if(!empty($data['data'])){

            $record_model = new ClassRecord();

            foreach ($data['data'] as &$v){

                $v['create_time']  = date('Y-m-d H:i:s',$v['create_time']);

                if($v['type']==0){

                    $class_record = $record_model->dataInfo(['id'=>$v['record_id']]);
                    //课程名字
                    $v['class_title'] = $class_record['title'];
                    //备注
                    $v['text']        = $class_record['record_text'];
                    //课程类型
                    $v['class_type']  = $class_record['class_type'];

                    $add_user         = !empty($class_record['add_user'])?$class_record['add_user']:$class_record['user_id'];
                    //操作者
                    $v['controller_name'] = lbUserName($add_user);

                }else{
                    //操作者
                    $v['controller_name'] = !empty($v['controller'])?lbUserName($v['controller']):'后台操作';

                }

            }

        }

        return $data;

    }





}